package cn.com.headfree.callback;

import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.callback.call.DictionaryServerCall;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @Title DirectoryUtils.java
 * @Package cn.com.headfree.callback
 * @Description 字典工具类
 * @author yangwang
 * @date 2019年7月23日 下午3:55:03
 */
@Component
public class DictionaryUtils {
    private static final Logger LOG = LogManager.getLogger(DictionaryUtils.class);

    /* 项目资金类型Map */
    private static Map<String, String> MONEY_TYPE_MAP;

    /* 年度Map */
    private static Map<String, String> YEAR_MAP;

    /* 项目类型Map */
    private static Map<String, String> TYPE_ID_MAP;

    /* 项目状态名称(0:未编制，1:正常；2：暂停；3：延期)Map */
    private static Map<String, String> STATE_MAP;

    /* 审核状态名称(0表示待审核，1表示通过，2表示驳回)Map */
    private static Map<String, String> VERIFY_STATU_MAP;

    /*
     * 审核操作类型名称(操作类型（0：新建；1：编辑； 2：编制申请；3：编制驳回；4：编制通过；5：变更申请；6：变更驳回；7：变更通过）)Map
     */
    private static Map<String, String> OPERATE_TYPE_MAP;

    /*
     * 所属公司(1：合肥恒卓； 2：国网南瑞；3：继远软件)Map
     */
    private static Map<String, String> COMPANY_TYPE_MAP;

    /*
     * 学历(1：博士； 2：硕士；3：研究生；4：本科；5：专科；6：专科以下)Map
     */
    private static Map<String, String> EDUCATION_TYPE_MAP;

    /**
     * 获取所有项目资金类型
     * 
     * @author yangwang
     * @param dictionaryServerCall
     * @return Map<String, String>
     */
    public static Map<String, String> getMONEY_TYPE_MAP(DictionaryServerCall dictionaryServerCall) {
        // 获取所有年度
        if (null == MONEY_TYPE_MAP) {
            Map<String, String> map = null;
            ResultData resultData = null;
            JSONArray jsonArray = null;
            JSONObject obj = null;
            resultData = dictionaryServerCall.findDictionaryByParentValue("project_manage", "project_moneytype");
            if (null != resultData) {
                LOG.info("获取所有项目资金类型数据为--" + resultData.toString());
                if (null != resultData.getData()) {
                    jsonArray = JSONArray.fromObject(resultData.getData());
                    if ((null != jsonArray) && (0 < jsonArray.size())) {
                        map = new HashMap<String, String>();
                        for (int i = 0; i < jsonArray.size(); i++) {
                            obj = (JSONObject) jsonArray.get(i);
                            if (null != obj) {
                                map.put(obj.getString("value"), obj.getString("name"));
                            }
                        }
                        DictionaryUtils.MONEY_TYPE_MAP = map;
                    }
                }
            }
        }
        return MONEY_TYPE_MAP;
    }

    /**
     * 获取所有年度
     * 
     * @author yangwang
     * @param dictionaryServerCall
     * @return Map<String, String>
     */
    public static Map<String, String> getYEAR_MAP(DictionaryServerCall dictionaryServerCall) {
        // 获取所有年度
        if (null == YEAR_MAP) {
            Map<String, String> map = null;
            ResultData resultData = null;
            JSONArray jsonArray = null;
            JSONObject obj = null;
            resultData = dictionaryServerCall.findDictionaryByParentValue("project_manage", "project_year");
            if (null != resultData) {
                LOG.info("获取所有年度数据为--" + resultData.toString());
                if (null != resultData.getData()) {
                    jsonArray = JSONArray.fromObject(resultData.getData());
                    if ((null != jsonArray) && (0 < jsonArray.size())) {
                        map = new HashMap<String, String>();
                        for (int i = 0; i < jsonArray.size(); i++) {
                            obj = (JSONObject) jsonArray.get(i);
                            if (null != obj) {
                                map.put(obj.getString("value"), obj.getString("name"));
                            }
                        }
                        DictionaryUtils.YEAR_MAP = map;
                    }
                }
            }
        }
        return YEAR_MAP;
    }

    /**
     * 获取所有项目类型
     * 
     * @author yangwang
     * @param dictionaryServerCall
     * @return Map<String, String>
     */
    public static Map<String, String> getTYPE_ID_MAP(DictionaryServerCall dictionaryServerCall) {
        // 获取所有项目类型
        if (null == TYPE_ID_MAP) {
            Map<String, String> map = null;
            ResultData resultData = null;
            JSONArray jsonArray = null;
            JSONObject obj = null;
            resultData = dictionaryServerCall.findDictionaryByParentValue("project_manage", "project_typeid");
            if (null != resultData) {
                LOG.info("获取所有项目类型数据为--" + resultData.toString());
                if (null != resultData.getData()) {
                    jsonArray = JSONArray.fromObject(resultData.getData());
                    if ((null != jsonArray) && (0 < jsonArray.size())) {
                        map = new HashMap<String, String>();
                        for (int i = 0; i < jsonArray.size(); i++) {
                            obj = (JSONObject) jsonArray.get(i);
                            if (null != obj) {
                                map.put(obj.getString("value"), obj.getString("name"));
                            }
                        }
                        DictionaryUtils.TYPE_ID_MAP = map;
                    }
                }
            }
        }
        return TYPE_ID_MAP;
    }

    /**
     * 获取所有项目状态
     * 
     * @author yangwang
     * @param dictionaryServerCall
     * @return Map<String, String>
     */
    public static Map<String, String> getSTATE_MAP(DictionaryServerCall dictionaryServerCall) {
        // 获取所有项目状态
        if (null == STATE_MAP) {
            Map<String, String> map = null;
            ResultData resultData = null;
            JSONArray jsonArray = null;
            JSONObject obj = null;
            resultData = dictionaryServerCall.findDictionaryByParentValue("project_manage", "project_status");
            if (null != resultData) {
                LOG.info("获取所有项目状态数据为--" + resultData.toString());
                if (null != resultData.getData()) {
                    jsonArray = JSONArray.fromObject(resultData.getData());
                    if ((null != jsonArray) && (0 < jsonArray.size())) {
                        map = new HashMap<String, String>();
                        for (int i = 0; i < jsonArray.size(); i++) {
                            obj = (JSONObject) jsonArray.get(i);
                            if (null != obj) {
                                map.put(obj.getString("value"), obj.getString("name"));
                            }
                        }
                        DictionaryUtils.STATE_MAP = map;
                    }
                }
            }
        }
        return STATE_MAP;
    }

    /**
     * 获取所有审核状态
     * 
     * @author yangwang
     * @param dictionaryServerCall
     * @return Map<String, String>
     */
    public static Map<String, String> getVERIFY_STATU_MAP(DictionaryServerCall dictionaryServerCall) {
        // 获取所有审核状态
        if (null == VERIFY_STATU_MAP) {
            Map<String, String> map = null;
            ResultData resultData = null;
            JSONArray jsonArray = null;
            JSONObject obj = null;
            resultData = dictionaryServerCall.findDictionaryByParentValue("project_manage", "verify_statu");
            if (null != resultData) {
                LOG.info("获取所有审核状态数据为--" + resultData.toString());
                if (null != resultData.getData()) {
                    jsonArray = JSONArray.fromObject(resultData.getData());
                    if ((null != jsonArray) && (0 < jsonArray.size())) {
                        map = new HashMap<String, String>();
                        for (int i = 0; i < jsonArray.size(); i++) {
                            obj = (JSONObject) jsonArray.get(i);
                            if (null != obj) {
                                map.put(obj.getString("value"), obj.getString("name"));
                            }
                        }
                        DictionaryUtils.VERIFY_STATU_MAP = map;
                    }
                }
            }
        }
        return VERIFY_STATU_MAP;
    }

    /**
     * 获取所有审核操作类型
     * 
     * @author yangwang
     * @param dictionaryServerCall
     * @return Map<String, String>
     */
    public static Map<String, String> getOPERATE_TYPE_MAP(DictionaryServerCall dictionaryServerCall) {
        // 获取所有审核操作类型
        if (null == OPERATE_TYPE_MAP) {
            Map<String, String> map = null;
            ResultData resultData = null;
            JSONArray jsonArray = null;
            JSONObject obj = null;
            resultData = dictionaryServerCall.findDictionaryByParentValue("project_manage", "operate_type");
            if (null != resultData) {
                LOG.info("获取所有审核操作类型数据为--" + resultData.toString());
                if (null != resultData.getData()) {
                    jsonArray = JSONArray.fromObject(resultData.getData());
                    if ((null != jsonArray) && (0 < jsonArray.size())) {
                        map = new HashMap<String, String>();
                        for (int i = 0; i < jsonArray.size(); i++) {
                            obj = (JSONObject) jsonArray.get(i);
                            if (null != obj) {
                                map.put(obj.getString("value"), obj.getString("name"));
                            }
                        }
                        DictionaryUtils.OPERATE_TYPE_MAP = map;
                    }
                }
            }
        }
        return OPERATE_TYPE_MAP;
    }

    /**
     * 获取所属公司类型
     * 
     * @author yangwang
     * @param dictionaryServerCall
     * @return Map<String, String>
     */
    public static Map<String, String> getCOMPANY_TYPE_MAP(DictionaryServerCall dictionaryServerCall) {
        // 获取所有审核操作类型
        if (null == COMPANY_TYPE_MAP) {
            Map<String, String> map = null;
            ResultData resultData = null;
            JSONArray jsonArray = null;
            JSONObject obj = null;
            resultData = dictionaryServerCall.findDictionaryByParentValue("project_manage", "subordinate_company");
            if (null != resultData) {
                LOG.info("获取所有审核操作类型数据为--" + resultData.toString());
                if (null != resultData.getData()) {
                    jsonArray = JSONArray.fromObject(resultData.getData());
                    if ((null != jsonArray) && (0 < jsonArray.size())) {
                        map = new HashMap<String, String>();
                        for (int i = 0; i < jsonArray.size(); i++) {
                            obj = (JSONObject) jsonArray.get(i);
                            if (null != obj) {
                                map.put(obj.getString("value"), obj.getString("name"));
                            }
                        }
                        DictionaryUtils.COMPANY_TYPE_MAP = map;
                    }
                }
            }
        }
        return COMPANY_TYPE_MAP;
    }

    /**
     * 获取学历类型
     * 
     * @author yangwang
     * @param dictionaryServerCall
     * @return Map<String, String>
     */
    public static Map<String, String> getEDUCATION_TYPE_MAP(DictionaryServerCall dictionaryServerCall) {
        // 获取所有审核操作类型
        if (null == EDUCATION_TYPE_MAP) {
            Map<String, String> map = null;
            ResultData resultData = null;
            JSONArray jsonArray = null;
            JSONObject obj = null;
            resultData = dictionaryServerCall.findDictionaryByParentValue("project_manage", "education");
            if (null != resultData) {
                LOG.info("获取所有审核操作类型数据为--" + resultData.toString());
                if (null != resultData.getData()) {
                    jsonArray = JSONArray.fromObject(resultData.getData());
                    if ((null != jsonArray) && (0 < jsonArray.size())) {
                        map = new HashMap<String, String>();
                        for (int i = 0; i < jsonArray.size(); i++) {
                            obj = (JSONObject) jsonArray.get(i);
                            if (null != obj) {
                                map.put(obj.getString("value"), obj.getString("name"));
                            }
                        }
                        DictionaryUtils.EDUCATION_TYPE_MAP = map;
                    }
                }
            }
        }
        return EDUCATION_TYPE_MAP;
    }

    /**
     * 获取所有项目类型
     * 
     * @author yangwang
     * @param dictionaryServerCall
     * @return Map<String, String>
     */
    public static Map<String, String> getCustomMap(DictionaryServerCall dictionaryServerCall, String customId) {

        Map<String, String> map = new HashMap<String, String>();
        switch (customId) {
        case "project_moneytype":
            map = getMONEY_TYPE_MAP(dictionaryServerCall);
            break;
        case "project_year":
            map = getYEAR_MAP(dictionaryServerCall);
            break;
        case "project_typeid":
            map = getTYPE_ID_MAP(dictionaryServerCall);
            break;
        case "project_status":
            map = getSTATE_MAP(dictionaryServerCall);
            break;
        case "verify_statu":
            map = getVERIFY_STATU_MAP(dictionaryServerCall);
            break;
        case "operate_type":
            map = getOPERATE_TYPE_MAP(dictionaryServerCall);
            break;
        case "company_type":
            map = getCOMPANY_TYPE_MAP(dictionaryServerCall);
            break;
        case "education_type":
            map = getEDUCATION_TYPE_MAP(dictionaryServerCall);
            break;
        default:
            break;
        }
        return map;
    }
}
